﻿    var loading = new load();
    loading.show("/UsersMange/Load", 1);
    //把json转换为指定字符串
    function showlist(load) {
        var str;
        var s=load["page"];
        showpage(s, "/UsersMange/Load");
        var e = load["list"];
        for (var i = 0; i < e.length; i++) {
            str = str + "<tr><td><input id='cbox' data-id='" + e[i].user_id + "' data-key='user_id' type='checkbox' /></td>" +
             "<td title='会员姓名' data-key='user_name'>" + e[i].user_name + "</td>" +
             "<td title='会员电话' data-key='user_tel'>" + e[i].user_tel + "</td>" +
             "<td title='卡内余额' data-key='user_money'>" + e[i].user_money + "</td>" +
             "<td title='注册时间' data-key='user_time'>" + e[i].user_time + "</td></tr>";
        }
        $(".loading").hide();
        return str;
    }
    //获取选中的数据
    function date() {
        var lists = [];
        //var list=[user_id];
        $("input[id='cbox']:checked").each(function (i) {
            var list = {};
            list.user_id = $(this).attr("data-id");
            list.user_name = $(this).parent().parent().find("td[data-key='user_name']").text();
            list.user_tel = $(this).parent().parent().find("td[data-key='user_tel']").text();
            list.user_money = $(this).parent().parent().find("td[data-key='user_money']").text();
            list.user_time = $(this).parent().parent().find("td[data-key='user_time']").text();
            lists.push(list);
        })
        return lists;
    }
    
    //添加
    function add() {
        layer.open({
            type: 1,
            skin: 'layui-layer-rim',
            title:"用户添加",
            area: ['350px', '300px'],
            content: $('#Dlgshow'),
            btn: ["保存", "关闭"],
            yes: function (index, layero) {
                if (!tllchange()) {
                    return;
                }
                var list = {};
                if ($(layero).find("input[name='user_name']").val() == "") {
                    layer.msg("会员姓名不能为空");
                    return;
                }
                if ($(layero).find("input[name='user_tel']").val() == "") {
                    layer.msg("电话号码不能为空");
                    return;
                }
                list.user_name = $(layero).find("input[name='user_name']").val();
                list.user_tel = $(layero).find("input[name='user_tel']").val();
                list.user_money = 0;
                console.log(list.user_name+list.user_tel);
                $.post("/UsersMange/Add", { json: JSON.stringify(list) }, function (date) {
                    console.log(date);
                    if (date.s == "ok") {
                        layer.msg(date.msg);
                    } else {
                        layer.msg(date.msg);
                    }
                    loading.show("/UsersMange/Load", 1);
                    console.log("---");
                }, "json");
                layer.close(index);
            },
            cancel: function (index) {
                layer.close(index);
                
            }
        });
    }
    //编辑
    function edit() {
        var lists = date();
        if (lists.length == 0) {
            layer.msg("请选择一条哟！！");
            return;
        } else if(lists.length!=1){
            layer.msg("一条数据，不要贪杯哦");
        } else {
            
            $("#Dlgshow").find("input[name='user_name']").val(lists[0].user_name);
            $("#Dlgshow").find("input[name='user_tel']").val(lists[0].user_tel);
            $("#Dlgshow").find("input[name='user_money']").val(lists[0].user_money);
            layer.open({
                type: 1,
                skin: 'layui-layer-rim',
                title: "用户编辑",
                area: ['350px', '300px'],
                content: $('#Dlgshow'),
                btn: ["保存", "关闭"],
                yes: function (index, layero) {
                    if (!tllchange()) {
                        return;
                    }
                    if ($(layero).find("input[name='user_name']").val() == "") {
                        layer.msg("会员姓名不能为空");
                        return;
                    }
                    if ($(layero).find("input[name='user_tel']").val() == "") {
                        layer.msg("电话号码不能为空");
                        return;
                    }
                    //console.log("猜猜我执行没有")
                    lists[0].user_name=$(layero).find("input[name='user_name']").val();
                    lists[0].user_tel = $(layero).find("input[name='user_tel']").val();
                    $.post("/UsersMange/Edit", { json: JSON.stringify(lists[0]) }, function (date) {
                        console.log(date);
                        layer.msg(date.msg);
                        loading.show("/UsersMange/Load", 1);
                    }, "json");
                    layer.close(index);
                },
                cancel: function (index) {
                    layer.close(index);

                }
            });
        }
    }
    // 删除
    function del() {
        var lists = date();
        if (lists.length == 0) {
            layer.msg("请至少选择一条哟！！");
            return;
        }
        layer.confirm('确定取消选中信息？', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.post("/UsersMange/Del", { json: JSON.stringify(lists) }, function (date) {
                console.log(date);
                layer.msg(date.msg);
                loading.show("/UsersMange/Load", 1);
                console.log("---");
            }, "json");
        });
        
    }
    //改变页码的时候
    function pg_change(page) {
        if (isNaN(page)) {
            layer.msg("请数字");
            return;
        }else if (page <= 0) {
            layer.msg("请输入大于等于1的页数");
            return;
        }
        loading.show("/UsersMange/Load", page);
    }
    //判断用户电话号码是否已注册
    function tllchange() {
        var is=true;
        var tel = $("#Dlgshow").find("input[name='user_tel']").val();
        $.ajax({
            url: "/UsersMange/istell",
            data: { tell: tel },
            async: false,
            type:"post",
            success: function (date) {
                console.log(date);
                //存在
                if (date.s == "n") {
                    layer.tips(date.msg, "#tel");
                    is = false;
                }
            }
        });
        return is;
    }
